package com.hrms.controller.admin;

import com.hrms.entity.Customer;
import com.hrms.entity.User;
import com.hrms.http.ResponseResult;
import com.hrms.service.CustomerService;
import com.hrms.service.UserService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;

@Transactional
@RestController
@RequestMapping("/admin/usermanage/newuser")
public class AdminNewUserController {
    // 管理员新建用户

    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/add")
    public ResponseResult<Object> add(@RequestBody EntityUtil entity) {
        // 新建用户

        Customer customer = new Customer();

        User user = new User();
        user.setUser_id(entity.getUser_id());
        user.setUser_name(entity.getUser_name());
        user.setUser_password(entity.getUser_password());
        user.setUser_status("离线");

        // 用户真实信息要么全部为空，要么全部不为空
        // 全部为空
        if(entity.getCustomer_id().isEmpty() && entity.getCustomer_name().isEmpty() &&
                entity.getCustomer_phone().isEmpty() && entity.getCustomer_sex().isEmpty()) {
            user.setCustomer_id("无");
        }
        // 全部不为空
        else if(!entity.getCustomer_id().isEmpty() && !entity.getCustomer_name().isEmpty() &&
                !entity.getCustomer_phone().isEmpty() && !entity.getCustomer_sex().isEmpty()) {
            user.setCustomer_id(entity.getCustomer_id());

            customer.setCustomer_id(entity.getCustomer_id());
            customer.setCustomer_name(entity.getCustomer_name());
            customer.setCustomer_phone(entity.getCustomer_phone());
            customer.setCustomer_sex(entity.getCustomer_sex());
        }
        else {
            return new ResponseResult<>("ERROR", null);
        }

        // user_id已存在
        if(userService.accountExist(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_user_id", null);
        }

        // user_name已存在
        if(userService.nameExist(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_user_name", null);
        }

        // 用户真实信息已给出
        if(!Objects.equals(user.getCustomer_id(), "无")) {
            // 新建用户真实信息
            if(!customerService.add(customer)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        // 新建用户
        if(!userService.addUser(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 绑定用户真实信息
        if(!userService.addCustomer(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
